let user = localStorage.user && JSON.parse(localStorage.user)
let body = document.querySelector('.body')
let dropdown = document.querySelector('.dropdown-lift')
let dropdowns = document.querySelector('.dropdown-right')
let we = document.querySelector('.pic1')
let you = document.querySelector('.pic2')
let time = document.querySelector('.times')

//发送请求，获取用户数据
renderContents()
async function renderContents() {

  console.log()
  let res = await axios({
    method: 'get',
    url: '/api/user',
    params: {
      userId: user.userId
    }
  })
  //渲染数据
  let data = res.data.data
  console.log(data);

  if (data == null) {
    return
  }
  renderUserList2(data)
  function renderUserList2(data) {
    let reminds = '';
    reminds += `
        <a href="" class="title">${data.username}</a>
        <span class="caret"></span>
        <ul class="userdisplay">
            <li>
                <a href="../lk/my-diary.html">我的日记</a>
            </li>
            <li>
                <a href="guanzhudereng.html">我关注的人</a>
            </li>
            <li>
                <a href="">设置</a>
            </li>
            <li>
                <a href="../dc/login.html">退出</a>
            </li>
        </ul>
        `
    dropdowns.innerHTML = reminds
  }

  let remind = '';
  remind += `
                    <a href="tixing.html" class="title">提醒(${0})</a>
                    <span class="caret"></span>
                    <ul class="msg">
                        <li>没有提醒</li>
                    </ul>
                `
  dropdown.innerHTML = remind






  getNotebooks()
  async function getNotebooks() {

    // 发请求 拿关注用户的Id
    let res = await axios({
      method: 'GET',
      url: '/api/user',
      params: {
        userId: user.userId
      }
    });
    let data = res.data.data.idolId//关注用户id
    console.log(111, data);
    if (data.length == 0) {
      let html = ''
      html = `
      <div class="bodys">
      <span>暂时没有日记</span>
      </div>
      `
      body.innerHTML = html
    }

    let arr = [];


    // 循环获取所有关注用户的日记，添加到 arr

    // 对 arr 中的日记安装时间进行排序

    // 将 arr 中的日记渲染至页面


    //获取关注用户
    for (const item of data) {
      let res1 = await axios({
        method: 'GET',
        url: '/api/user',
        params: {
          userId: `${item}`
        }
      });

      let id = res1.data.data
      console.log(id);
      //拿关注用户的日记本
      for (const item of data) {
        if (data.length = 0) {
          let html = ''
          html = `
      <div class="bodys">
      <span>暂时没有日记</span>
      </div>
      `
          body.innerHTML = html
        } else {
          let res = await axios({
            method: 'GET',
            url: '/api/diarybook',
            params: {
              authorId: `${item}`,
              user: user.userId
              // user: "632595e0e24deb53641c4b2b"
            }
          });

          let data = res.data.data.diarybooks//日记本id
          console.log(333, data);

          // 拿取关注用户的日记
          if (data == null) {
            let html = ''
            html = `
            <div class="bodys">
            <span>暂时没有日记</span>
            </div>
            `
            body.innerHTML = html
          } else {
            let html = ''
            //拿关注用户日记本里的的日记
            for (const item of data) {
              // console.log(5555,item);
              let res = await axios({
                method: 'GET',
                url: '/api/diaryItem',
                params: {
                  diarybookId: `63228a7eae88eecc3c4c9115`,
                }
              });
              let data = res.data.data//关注用户日记本里的的日记  
              //  console.log(11111,data);

              // 拿取关注用户的日记
              if (!data) {
                // let html  = ''
                html = `
                          <div class="bodys">
                          <span>暂时没有日记</span>
                          </div>
                          `
                body.innerHTML = html
                return;
              }

              let html = ''
              //存id
              let authorIds = {

              }
              for (const item of data) {
                // console.log(33333,item);
                html += `
                              <div class="bodys">
                              <a href="">
                                  <img class="touxiang" src="http://localhost:8000${id.headphoto}" alt="">
                              </a> 
                              <div class="body-text">
                                  <div class="text">
                                      <a href="">${id.username}</a>
                                      ${item.createAt} am 
                                      <a href="">  《obj》</a>
                                  </div>
                                 <div class="daytext">
                                  <p>${item.content} </p>
                                 </div>
                                  <div>
                                      <a href="../ys/reply.html" class="return">${item.replyId.length} 回复</a>
                                  </div>
                              </div>
                          </div>
                               `
              }
              body.innerHTML = html

            }
          }
        }
      }

    }

  }


  getTime()
  async function getTime() {
    let res1 = await axios({
      method: "GET",
      url: "/api/user",
      params: {
        userId: user.userId
      },
    });
    let date = new Date();
    // 获取日期：年月日
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    this.nowDate = month + "/" + day;
    this.nowYear = "/" + year;

    // 获取星期几
    const weeks = new Array(
      "星期日",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六"
    );
    this.nowWeek = weeks[new Date().getDay()];
    nowWeek = this.nowWeek;
    nowDate = this.nowDate;
    nowYear = this.nowYear;

    // console.log(res1);
    userName = res1.data.data.username;
    let imgSrc = res1.data.data.headphoto;
    let beginTime = res1.data.data.createAt
      .replace(/[a-zA-Z]+/g, "")
      .slice(0, 10);
    let description = res1.data.data.description;
    let html = `
        <span>${nowDate}</span>
        <span class="time">
            <span>${nowWeek}</span>
            <span>${nowYear}</span>
        </span>
    `
    time.innerHTML = html
  }



  //关注用户

  renderContents21()
  async function renderContents21() {
    let res = await axios({
      method: 'get',
      url: '/api/user/',
      params: {
        userId: user.userId
      }
    })
    //关注用户
    let data = res.data.data.idolId
    console.log(77, data, data.length);
    //遍历关注用户ID

    if (data.length == 0) {
      return
    } else {
      let html1 = '';
      for (const item of data) {
        // console.log(item);
        let res22 = await axios({
          method: 'get',
          url: '/api/user',
          params: {
            userId: `${item}`

          }
        })

        let user = res22.data.data
        // console.log(9898, user);
        html1 += `
        <div class="pic pic">
            <a href="">
                <img src="http://localhost:8000${user.headphoto}" alt="">
            </a>
            <a href="">
            ${user.username}
            </a>
        </div>
         `
        we.innerHTML = html1
      }
    }
  }



  //粉丝

  renderContents11()
  async function renderContents11() {
    let res = await axios({
      method: 'get',
      url: '/api/user/',
      params: {
        userId: user.userId
        // userId: "632595e0e24deb53641c4b2b"
      }
    })
    //粉丝id
    let data = res.data.data.fansId
    // console.log(77,data);
    //遍历粉丝ID

    for (const item of data) {
      // console.log(item);
      let res22 = await axios({
        method: 'get',
        url: '/api/user',
        params: {
          userId: `${item}`

        }
      })

      let user = res22.data.data
      //   console.log(9898, user);

      let html2 = '';
      html2 += `
        <div class="pic pic2">
            <a href="">
                <img src="http://localhost:8000${user.headphoto}" alt="">
            </a>
            <a href="">
            ${user.username}
            </a>
        </div>
         `
      you.innerHTML = html2
    }
  }
}